import 'package:flutter_admin/models/user_info.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/models/odoo/stock_picking.dart';
import 'package:flutter_admin/utils/store_util.dart';
import '../../../../generated/l10n.dart';

class WorkshopMaterialDialog extends StatefulWidget {
  WorkshopMaterialDialog();

  @override
  _WorkshopMaterialDialogState createState() => _WorkshopMaterialDialogState();
}

class _WorkshopMaterialDialogState extends State<WorkshopMaterialDialog> {
  // late List<Widget> children;
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  StockPicking stockPicking = StockPicking();

  //工厂名称
  List<SelectOptionVO> locationIdList = [];
  //车间名称
  List<SelectOptionVO> workshopIdList = [];

  @override
  void initState() {
    super.initState();
    _loadSelectVO();
  }

  _loadSelectVO() async {
    UserInfo userInfo = StoreUtil.getCurrentUserInfo();
    if (userInfo.major != null) {
      this.workshopIdList = await DynamicDataApi.getModelSelect(
          // active: false,
          model: "hr.department",
          name: "complete_name",
          key: 'parent_id',
          operator: "!=",
          val: null);
    } else {
      locationIdList = await DynamicDataApi.getModelSelect(
          active: false,
          model: "stock.location",
          name: "complete_name",
          key: 'workshop_id',
          val: int.parse(userInfo.deptId!),
          key2: 'comment',
          val2: 'XBK');
      stockPicking.workshopId = userInfo.deptId;
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    UserInfo userInfo = StoreUtil.getCurrentUserInfo();
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          userInfo.major != null
              ? CrySelect(
                  label: '生产车间',
                  dataList: workshopIdList,
                  value: stockPicking.workshopId,
                  onSaved: (v) => {stockPicking.workshopId = v},
                  onChange: (v) async {
                    if (stockPicking.workshopId != v) {
                      formKey.currentState!.save();
                      stockPicking.workshopId = v;
                      stockPicking.locationDestId = null;
                      try {
                        locationIdList = await DynamicDataApi.getModelSelect(
                            active: false,
                            model: "stock.location",
                            name: "complete_name",
                            key: 'workshop_id',
                            val: int.parse(v),
                            key2: 'comment',
                            val2: 'XBK');
                      } catch (e) {
                        locationIdList = [];
                      }
                      setState(() {});
                    }
                  },
                )
              : CryInput(
                  width: 200,
                  label: '生产车间',
                  enable: false,
                  value: userInfo.deptName,
                ),
          CrySelect(
            label: '目标库房',
            dataList: locationIdList,
            value: stockPicking.locationDestId,
            onSaved: (v) => {stockPicking.locationDestId = v},
          ),
          CryInput(
            label: '入库单号',
            value: stockPicking.name,
            onSaved: (v) {
              stockPicking.name = v;
            },
          ),
          CryInput(
            label: '物料编码',
            value: stockPicking.productCode,
            onSaved: (v) {
              stockPicking.productCode = v;
            },
          ),
          CryInput(
            label: '物料名称',
            value: stockPicking.productName,
            onSaved: (v) {
              stockPicking.productName = v;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: "确定",
          onPressed: () {
            FormState form = formKey.currentState!;
            form.save();
            Navigator.pop(context, stockPicking);
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text("查询"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
